package example.com.zyfypt_618zgm.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.util.List;

import example.com.zyfypt_618zgm.R;
import example.com.zyfypt_618zgm.activity.CollectMod;
import example.com.zyfypt_618zgm.activity.CollectVideo;
import example.com.zyfypt_618zgm.bean.CollectResult;
import example.com.zyfypt_618zgm.bean.ModBean;

import static example.com.zyfypt_618zgm.activity.CollectMain.index1;

/**
 * Created by 小茗同学 on 2017/6/1.
 */

public class CollectAdapter extends RecyclerView.Adapter {
    private Context context;
    private LayoutInflater layoutInflater;//动态加载布局
    private List<CollectResult<ModBean>> list;//保存要显示的数据
    public static String mod1;
    public static int id1;
    public static int id2;
    public static String videopath1;
    public CollectAdapter(Context context) {
        this.context=context;
        layoutInflater=LayoutInflater.from(context);
    }
    public void setList(List<CollectResult<ModBean>> list){
        this.list=list;
        notifyDataSetChanged();
    }
    public class ViewHolder extends RecyclerView.ViewHolder{
        private ImageView imageView;
        private TextView tv_title,tv_descript,tv_time;
        public ViewHolder(View itemView) {
            super(itemView);
            imageView = (ImageView) itemView.findViewById(R.id.imageView);
            tv_title = (TextView) itemView.findViewById(R.id.textView);
            tv_descript = (TextView) itemView.findViewById(R.id.textView2);
            tv_time = (TextView) itemView.findViewById(R.id.textView3);
        }
    }
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = layoutInflater.inflate(R.layout.item, parent, false);
        return new CollectAdapter.ViewHolder(v);
    }
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        CollectResult<ModBean> moder = list.get(position);
        if (moder == null)
            return;
        CollectAdapter.ViewHolder viewholder = (CollectAdapter.ViewHolder) holder;
        Picasso.with(context).load("http://amicool.neusoft.edu.cn/Uploads/"+moder.getBean().getThumb()).placeholder(R.drawable.jiazai).into(viewholder.imageView);
        viewholder.tv_title.setText(moder.getBean().getName());
        viewholder.tv_descript.setText(moder.getBean().getDescription());
        viewholder.tv_time.setText(moder.getBean().getUpdate_time());
        viewholder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(index1==0)
                    mod1="article";
                else if(index1==1)
                    mod1="tware";
                else if(index1==2)
                    mod1="video";
                else if(index1==3)
                    mod1="tcase";
                else if(index1==4)
                    mod1="project";
                id1=list.get(position).getBean().getId();
                id2=list.get(position).getBean().getUserid();
                videopath1=list.get(position).getBean().getVideopath();
                if(index1==2){
                    Intent intent1=new Intent();
                    intent1.setClass(context,CollectVideo.class);
                    context.startActivity(intent1);
                }else {
                    Intent intent1=new Intent();
                    intent1.setClass(context,CollectMod.class);
                    context.startActivity(intent1);
                }
            }
        });

    }

    @Override
    public int getItemCount() {
        if(list==null)
            return 0;
        return list.size();
    }
}
